package com.uniledger.exchange.bank.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hbb20.CountryCodePicker;
import com.uniledger.exchange.R;
import com.uniledger.exchange.bank.Bank;
import com.uniledger.exchange.bank.BankCard;
import com.uniledger.exchange.bank.SelectBankActivity;
import com.uniledger.exchange.bank.view.BankNumEditText;

import butterknife.BindView;

/**
 *
 */
public class AddFragment extends CardFragment implements CountryCodePicker.OnCountryChangeListener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    public AddFragment() {
        // Required empty public constructor
    }

//    @BindView(R.id.edit_card_holder)
//    EditText accountName;

    @BindView(R.id.select_bank)
    TextView select_bank;

    @BindView(R.id.edit_card_no)
    BankNumEditText cardNumber;

    @BindView(R.id.select_country)
    CountryCodePicker ccp;

    public static AddFragment newInstance(String param1, String param2) {
        AddFragment fragment = new AddFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ccp.setOnCountryChangeListener(this::onCountrySelected);
        select_bank.setOnClickListener(this::onClick);
    }

    /**
     * 选择银行
     */

    void selectBank() {
        SelectBankActivity.start(getActivity());
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.select_bank) {
            selectBank();
        } else {
//            String name = accountName.getText().toString();
            String number = cardNumber.getBankNum();
            if (!TextUtils.isEmpty(number)) {
                listener.add("AAA", number);
            } else {

            }
        }
    }

    @Override
    public int getViewResourceId() {
        return R.layout.fragment_add;
    }

    /**
     * 选择国家
     */
    @Override
    public void onCountrySelected() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == SelectBankActivity.BANK_SELECT_CODE && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                BankCard bank = data.getParcelableExtra("Bank");
                select_bank.setText(bank.bank);
            }
        }

    }
}
